package cn.ht.jywrwater.module.ht.controller.admin.waterpipenetmaintain;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.waterpipenetmaintain.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterpipenetmaintain.WaterPipenetMaintainDO;
import cn.ht.jywrwater.module.ht.convert.waterpipenetmaintain.WaterPipenetMaintainConvert;
import cn.ht.jywrwater.module.ht.service.waterpipenetmaintain.WaterPipenetMaintainService;

@Tag(name = "管理后台 - 管网养护")
@RestController
@RequestMapping("/ht/water-pipenet-maintain")
@Validated
public class WaterPipenetMaintainController {

    @Resource
    private WaterPipenetMaintainService waterPipenetMaintainService;

    @PostMapping("/create")
    @Operation(summary = "创建管网养护")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:create')")
    public CommonResult<Long> createWaterPipenetMaintain(@Valid @RequestBody WaterPipenetMaintainCreateReqVO createReqVO) {
        return success(waterPipenetMaintainService.createWaterPipenetMaintain(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新管网养护")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:update')")
    public CommonResult<Boolean> updateWaterPipenetMaintain(@Valid @RequestBody WaterPipenetMaintainUpdateReqVO updateReqVO) {
        waterPipenetMaintainService.updateWaterPipenetMaintain(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除管网养护")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:delete')")
    public CommonResult<Boolean> deleteWaterPipenetMaintain(@RequestParam("id") Long id) {
        waterPipenetMaintainService.deleteWaterPipenetMaintain(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得管网养护")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:query')")
    public CommonResult<WaterPipenetMaintainRespVO> getWaterPipenetMaintain(@RequestParam("id") Long id) {
        WaterPipenetMaintainDO waterPipenetMaintain = waterPipenetMaintainService.getWaterPipenetMaintain(id);
        return success(WaterPipenetMaintainConvert.INSTANCE.convert(waterPipenetMaintain));
    }

    @GetMapping("/list")
    @Operation(summary = "获得管网养护列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:query')")
    public CommonResult<List<WaterPipenetMaintainRespVO>> getWaterPipenetMaintainList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterPipenetMaintainDO> list = waterPipenetMaintainService.getWaterPipenetMaintainList(ids);
        return success(WaterPipenetMaintainConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得管网养护分页")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:query')")
    public CommonResult<PageResult<WaterPipenetMaintainRespVO>> getWaterPipenetMaintainPage(@Valid WaterPipenetMaintainPageReqVO pageVO) {
        PageResult<WaterPipenetMaintainDO> pageResult = waterPipenetMaintainService.getWaterPipenetMaintainPage(pageVO);
        return success(WaterPipenetMaintainConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出管网养护 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterPipenetMaintainExcel(@Valid WaterPipenetMaintainExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterPipenetMaintainDO> list = waterPipenetMaintainService.getWaterPipenetMaintainList(exportReqVO);
        // 导出 Excel
        List<WaterPipenetMaintainExcelVO> datas = WaterPipenetMaintainConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "管网养护.xls", "数据", WaterPipenetMaintainExcelVO.class, datas);
    }

    @GetMapping("/pipenetMaintainDashboard")
    @Operation(summary = "获得管网养护专题显示")
//    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
//    @PreAuthorize("@ss.hasPermission('ht:water-pipenet-maintain:query')")
    public CommonResult<WaterPipenetMaintainVO> pipenetMaintainDashboard() {
        WaterPipenetMaintainVO waterPipenetMaintainVO = waterPipenetMaintainService.pipenetMaintainDashboard();
        return success(waterPipenetMaintainVO);
    }

}
